package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.user.service.dto.user.OrganizationQuotainfoDTO;
import com.bestcem.xm.user.service.dto.user.QuotaInfoQueryDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * grpc请求出入参转化类
 *
 * @author jishuo.yan <jishuo.yan@idiaoyan.com>
 * @date 2022/3/8 16:29
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrgQuotaConverter {

    /**
     * grpc请求入参转换成本地入参：QuotaInfoQuery转为QuotaInfoQueryDTO
     *
     * @param quotaQuery 开放平台查询限额信息入参
     * @return com.bestcem.xm.user.service.dto.user.QuotaInfoQueryDTO
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/28 14:42
     */
    public static QuotaInfoQueryDTO platformQuotaQueryToQuotaInfoQueryDTO(PlatformQuotaQuery quotaQuery) {
        QuotaInfoQueryDTO dto = new QuotaInfoQueryDTO();
        dto.setPlatform(quotaQuery.getPlatformCode());
        dto.setOrgId(quotaQuery.hasOrgId() ? quotaQuery.getOrgId() : null);
        dto.setProjectGt(quotaQuery.hasProjectGt() ? quotaQuery.getProjectGt() : null);
        dto.setProjectLte(quotaQuery.hasProjectLte() ? quotaQuery.getProjectLte() : null);
        dto.setRespondentGt(quotaQuery.hasSurveyGt() ? quotaQuery.getSurveyGt() : null);
        dto.setRespondentLte(quotaQuery.hasSurveyLte() ? quotaQuery.getSurveyLte() : null);
        return dto;
    }

    /**
     * grpc请求入参转换为本地入参：quotaInfo转为OrganizationQuotainfoDTO
     *
     * @param param 创建公司限额信息入参
     * @return OrganizationQuotainfoDTO
     * @author yanjishuo <jishuo.yan@idiaoyan.cn>
     * @date 2022/05/11 18:26
     */
    public static OrganizationQuotainfoDTO quotaCreateParamToDto(QuotaCreateParam param) {
        OrganizationQuotainfoDTO orgQuotainfoDTO = new OrganizationQuotainfoDTO();
        orgQuotainfoDTO.setActiveFlag(Boolean.TRUE.equals(param.getActive()) ? 1 : 0);
        orgQuotainfoDTO.setProjectQuota(param.getProject());
        orgQuotainfoDTO.setRespondentQuota(param.getSurvey());
        orgQuotainfoDTO.setProjectTotal(param.getProjectTotal());
        orgQuotainfoDTO.setRespondentTotal(param.getSurveyTotal());
        orgQuotainfoDTO.setUserTotal(param.getUserTotal());
        orgQuotainfoDTO.setMemberIndTotal(param.getMemberIndTotal());
        orgQuotainfoDTO.setDatasourceTotal(param.getBiDatasourceTotal());
        orgQuotainfoDTO.setOrgId(param.getOrgId());
        return orgQuotainfoDTO;
    }

    /**
     * grpc请求入参转换为本地入参：quotaInfo转为OrganizationQuotainfoDTO
     *
     * @param param 更新公司限额信息入参
     * @return OrganizationQuotainfoDTO
     * @author yanjishuo <jishuo.yan@idiaoyan.cn>
     * @date 2022/05/11 18:26
     */
    public static OrganizationQuotainfoDTO quotaUpdateParamToDto(QuotaUpdateParam param) {
        OrganizationQuotainfoDTO orgQuotainfoDTO = new OrganizationQuotainfoDTO();
        if (param.hasActive()) {
            orgQuotainfoDTO.setActiveFlag(Boolean.TRUE.equals(param.getActive()) ? 1 : 0);
        }
        orgQuotainfoDTO.setProjectQuota(param.hasProject() ? param.getProject() : null);
        orgQuotainfoDTO.setRespondentQuota(param.hasSurvey() ? param.getSurvey() : null);
        orgQuotainfoDTO.setProjectTotal(param.hasProjectTotal() ? param.getProjectTotal() : null);
        orgQuotainfoDTO.setRespondentTotal(param.hasSurveyTotal() ? param.getSurveyTotal() : null);
        orgQuotainfoDTO.setUserTotal(param.hasUserTotal() ? param.getUserTotal() : null);
        orgQuotainfoDTO.setMemberIndTotal(param.hasMemberIndTotal() ? param.getMemberIndTotal() : null);
        orgQuotainfoDTO.setDatasourceTotal(param.hasBiDatasourceTotal() ? param.getBiDatasourceTotal() : null);
        orgQuotainfoDTO.setOrgId(param.hasOrgId() ? param.getOrgId() : null);
        return orgQuotainfoDTO;
    }

    /**
     * grpc请求入参转换为本地入参：quotaInfo转为OrganizationQuotainfoDTO
     *
     * @param param 查询公司限额信息入参
     * @return OrganizationQuotainfoDTO
     * @author yanjishuo <jishuo.yan@idiaoyan.cn>
     * @date 2022/05/11 18:26
     */
    public static OrganizationQuotainfoDTO quotaQueryParamToDto(QuotaQueryParam param) {
        OrganizationQuotainfoDTO orgQuotainfoDTO = new OrganizationQuotainfoDTO();
        orgQuotainfoDTO.setQuotainfoId(param.hasId() ? param.getId() : null);
        if (param.hasActive()) {
            orgQuotainfoDTO.setActiveFlag(Boolean.TRUE.equals(param.getActive()) ? 1 : 0);
        }
        orgQuotainfoDTO.setProjectQuota(param.hasProject() ? param.getProject() : null);
        orgQuotainfoDTO.setRespondentQuota(param.hasSurvey() ? param.getSurvey() : null);
        orgQuotainfoDTO.setProjectTotal(param.hasProjectTotal() ? param.getProjectTotal() : null);
        orgQuotainfoDTO.setRespondentTotal(param.hasSurveyTotal() ? param.getSurveyTotal() : null);
        orgQuotainfoDTO.setUserTotal(param.hasUserTotal() ? param.getUserTotal() : null);
        orgQuotainfoDTO.setMemberIndTotal(param.hasMemberIndTotal() ? param.getMemberIndTotal() : null);
        orgQuotainfoDTO.setDatasourceTotal(param.hasBiDatasourceTotal() ? param.getBiDatasourceTotal() : null);
        orgQuotainfoDTO.setOrgId(param.hasOrgId() ? param.getOrgId() : null);
        return orgQuotainfoDTO;
    }

    /**
     * 将本地处理结果转换成grpc请求响应参数：OrganizationQuotainfoDTO转为QuotaInfo
     *
     * @param dto 待转换信息
     * @return com.bestcem.bp.xm.user.grpc.v1.services.Quota
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/28 14:43
     */
    public static Quota organizationQuotainfoDtoToQuotaInfo(OrganizationQuotainfoDTO dto) {
        Quota.Builder builder = Quota.newBuilder();
        if (StringUtils.isNotBlank(dto.getQuotainfoId())) {
            builder.setId(dto.getQuotainfoId());
        }
        if (StringUtils.isNotBlank(dto.getOrgId())) {
            builder.setOrgId(dto.getOrgId());
        }
        if (Objects.nonNull(dto.getActiveFlag())) {
            builder.setActive(dto.getActiveFlag() == 1 ? Boolean.TRUE : Boolean.FALSE);
        }
        if (Objects.nonNull(dto.getProjectQuota())) {
            builder.setProject(dto.getProjectQuota());
        }
        if (Objects.nonNull(dto.getRespondentQuota())) {
            builder.setSurvey(dto.getRespondentQuota());
        }
        if (Objects.nonNull(dto.getProjectTotal())) {
            builder.setProjectTotal(dto.getProjectTotal());
        }
        if (Objects.nonNull(dto.getRespondentTotal())) {
            builder.setSurveyTotal(dto.getRespondentTotal());
        }
        if (Objects.nonNull(dto.getUserTotal())) {
            builder.setUserTotal(dto.getUserTotal());
        }
        if (Objects.nonNull(dto.getMemberIndTotal())) {
            builder.setMemberIndTotal(dto.getMemberIndTotal());
        }
        if (Objects.nonNull(dto.getDatasourceTotal())) {
            builder.setBiDatasourceTotal(dto.getDatasourceTotal());
        }
        return builder.build();
    }

    /**
     * 将本地处理结果转换成grpc请求响应参数：List<OrganizationQuotainfoDTO>转为List<QuotaInfo>
     *
     * @param dtos 待转换信息列表
     * @return java.util.List<com.bestcem.bp.xm.user.grpc.v1.services.Quota>
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/28 14:44
     */
    public static List<Quota> organizationQuotainfosDtoToQuotaInfos(List<OrganizationQuotainfoDTO> dtos) {
        List<Quota> quotaInfos = new ArrayList<>();
        if (Objects.isNull(dtos)) {
            return quotaInfos;
        }
        for (OrganizationQuotainfoDTO dto : dtos) {
            Quota quotaInfo = organizationQuotainfoDtoToQuotaInfo(dto);
            quotaInfos.add(quotaInfo);
        }
        return quotaInfos;
    }

}
